/*
 * =====================================================================================
 *
 *       Filename:  holes.cpp
 *
 *    Description:  Returns number of holes in character
 *
 *        Version:  1.0
 *        Created:  05/07/12 15:47:31
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <cv.h>
#include <highgui.h>
#include <vector>

using namespace cv;
using namespace std;

float holes(Mat src) {
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Mat temp;
    src.convertTo(temp, CV_8UC1);

    findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

    unsigned int i;
    float nHoles = 0;

    for (i = 0; i < contours.size(); i++) {
        if (hierarchy[i][3] != -1)
            nHoles += 1;
    }

    return nHoles;
}

//int main(int argc, char* argv[]) {
//    Mat image;
//    
//    image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
//    threshold(image, image, 100, 255, THRESH_BINARY);
//    
//    cout << holes(image) << endl;
//}
